/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 * The WebAttributeHolder is the implementation of an AttributeHolder, encapsulating a Http-request,a http-session 
 * and a static ApplicationRequest
 *
 */
public class WebAttributeHolder implements AttributeHolder {

	
	HttpServletRequest request;
	
	//TODO find the right way
	private static Map<String,Object> applicationContext = new ConcurrentHashMap<String, Object>();
	
	public WebAttributeHolder(HttpServletRequest request) {
		this.request = request;
	}
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#getApplicationAttribute(java.lang.String)
	 */

	public Object getApplicationAttribute(String key) {
		
		return applicationContext.get(key);
	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#getRequestAttribute(java.lang.String)
	 */

	public Object getRequestAttribute(String key) {
		return request.getAttribute(key);
	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#getRequestParameter(java.lang.String)
	 */

	public String getRequestParameter(String key) {
		return request.getParameter(key);
	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#getSessionAttribute(java.lang.String)
	 */

	public Object getSessionAttribute(String key) {
		HttpSession s = request.getSession(true);
//		if (s==null)
//			throw new RuntimeException("no session found");
		return s.getAttribute(key);
	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#setApplicationAttribute(java.lang.String, java.lang.Object)
	 */

	public void setApplicationAttribute(String key, Object value) {
		applicationContext.put(key, value);
	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#setRequestAttribute(java.lang.String, java.lang.Object)
	 */

	public void setRequestAttribute(String key, Object value) {
		request.setAttribute(key, value);

	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#setSessionAttribute(java.lang.String, java.lang.Object)
	 */

	public void setSessionAttribute(String key, Object value) {
		HttpSession s = request.getSession(true);
		if (s==null)
			throw new RuntimeException("no session found");
		s.setAttribute(key,value);

	}

	/* (non-Javadoc)
	 * @see org.myontology.controller.AttributeHolder#setSessionAttribute(java.lang.String, java.lang.Object)
	 */

	public String getSessionId() {
		HttpSession s = request.getSession(false);
		if (s==null)
			return null;
		return s.getId();

	}

	public HttpServletRequest getRequest() {
		return request;
	}
	
}
